<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="content">注册登录</block>
		</cu-custom>
		
		<view v-if="isInit">
		
			<view :style="'height:'+height+'px;'" class="text-center" style="width:100%;position:absolute;top:30%;">
				<view>
					<view class="cu-avatar round lg" :style="'width:200rpx;height:200rpx;background-color:white;background-repeat: no-repeat;background-size:90% 90%;background-image:url('+appinfo.logo+')'"></view>
				</view>
				<view class="text-lg text-bold margin-top-lg">
					{{appinfo.name}}欢迎您
				</view>
				<view class="margin-top-lg">
					<button class="cu-btn bg-red round shadow-blur" @tap="showInfoWin" style="width: 60%;">设置信息完成注册</button>
				</view>				
			</view>
			
			<!-- 设置信息部分 begin -->
			<view class="cu-modal margin-top-sm" :class="showUserinfo?'show':''">
				<view class="cu-dialog">
					<view class="cu-bar bg-white justify-end">
						<view class="content">注册登录</view>
						<view class="action" @tap="showUserinfo=false">
							<text class="cuIcon-close text-red"></text>
						</view>
					</view>				
						
					<view>						
						<view class="cu-form-group solid-bottom">
							<view class="title">您的昵称</view>
							<view style="width:75%;">
								<input class="text-left" placeholder="请输入您的昵称" name="input" type="nickname" @change="changeName" v-model="userinfo.name"></input>	
							</view>
						</view>
						<view class="cu-form-group padding-tb-xs solid-bottom">
							<view class="title">您的头像</view>
							<view style="text-align: left;width:75%;">
								<button style="width: 45px;height:45px;margin:0px;padding: 0px;" type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
									<image style="width: 100%;height:100%;" mode="scaleToFill" :src="userinfo.avatar"></image>
								</button>
							</view>
						</view>
						<view class="cu-bar bg-white flex justify-center">
							<button class="cu-btn bg-red round" style="width: 85%;" @tap="reg()">同意协议并完成注册</button>
						</view>
						<view class="cu-bar padding-lr-lg bg-white flex justify-start">
							<checkbox-group @change="userAgree">
								<checkbox value="true" style="transform:scale(0.6)"></checkbox>
							</checkbox-group>
							<text class="padding-top-sm" style="line-height: 200%;">已阅读并同意 
								<text class="text-blue" @tap="openAgreement">{{appinfo.name}}用户协议</text>、
								<text class="text-blue" @tap="openPrivacy">{{appinfo.name}}隐私政策</text>
							</text>
						</view>
						
					</view>
						
				</view>
			</view>
			<!-- 设置信息部分 end -->
		
		</view>
		
		<view class="cu-load load-modal" v-if="!isInit">
			<view class="gray-text">加载中...</view>
		</view>
		
	</view>
</template>

<script>	
	import { mapActions } from 'vuex';
	import * as confirm from '@/common/confirm.js'
	import * as upload from '@/common/upload.js'
	
	export default {
		data() {
			return {
				isInit:false,
				agree:false,
				height: this.s_h_nob,
				userinfo:{},
				appinfo:{},
				showUserinfo:false
			}
		},
		onLoad(){
			this.$api.post('/user/ckIsAuth').then(res => {
				this.userinfo=res.data				
				this.$api.post('/common/app_config').then(res => {
					this.appinfo=res.data
					this.isInit=true
				})
			})	
		},
		methods:{
			showInfoWin(){
				this.showUserinfo=true
			},
			onChooseavatar(e) {
				let avatarUrl = e.detail.avatarUrl
				let _this=this
				upload.uploadImg('/user/upload_avatar',avatarUrl,function(res){
					uni.showToast({
						title:"修改头像成功"
					})
					_this.userinfo.avatar=res
				},{})
			},
			changeName(e){
				this.userinfo.name=e.target.value
			},
			userAgree(e){
				if(e.detail.value.length>0){
					this.agree=true
				}else{
					this.agree=false
				}
			},
			openAgreement(){
				this.openPdf('https://smart1st.online/admin/202412/agreement.pdf');
			},
			openPrivacy(){
				this.openPdf('https://smart1st.online/admin/202412/privacy.pdf');
			},
			openPdf(url) {
			  uni.downloadFile({
			    url: url, // PDF文件的网络地址
			    success: (res) => {
			      if (res.statusCode === 200) {
			        let filePath = res.tempFilePath;
			        // 根据不同的平台可能需要将路径转换为平台可识别的路径
			        // 例如，在App端可能需要使用plus.io.convertLocalFileSystemURL
			        uni.openDocument({
			          filePath: filePath,
			          fileType: 'pdf', // 明确指定文件类型为pdf
			          success: function (res) {
			            console.log('打开PDF成功');
			          },
			          fail: function (err) {
			            console.error('打开PDF失败', err);
			          }
			        });
			      }
			    }
			  });
			},
			reg(){
				if(!this.agree){
					confirm.warn("请您同意协议才能完成注册!!");
					return;
				}
				this.$api.post('/user/reg',{name:this.userinfo.name}).then(res => {
					this.userinfo=res.data
					confirm.success("授权成功!!",()=>{
						this.showUserinfo=false
						uni.reLaunch({
							url:'/pages/login/login'
						})
					})					
				}).catch(()=>{
					confirm.error("授权错误!")
					this.showUserinfo=false
				})
			}
		}
	}
</script>